package cn.com.myshiro.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import Test.tools.Page;

import java.util.Map;
import java.util.HashMap;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.com.myshiro.bean.Sysuser;
import cn.com.myshiro.bean.Userrole;
import cn.com.myshiro.dto.ActiveUserDto;
import cn.com.myshiro.service.UserroleService;
import cn.com.myshiro.shiro.MD5;
import cn.com.myshiro.shiro.ShiroDbRealm;

@Controller
public class UserroleController {
	  private static Logger logger = LoggerFactory.getLogger(ShiroDbRealm.class);
      @Autowired
      UserroleService service;

      @RequestMapping(value = "/userroleView.do")
      public String userroleView(ModelMap modelMap,String pageNo, String choice, HttpSession session){
			int currentPage = 1;
			if (pageNo != null) {
				currentPage = Integer.parseInt(pageNo);
			}
			int countNum = service.countUserrole();
			Page _page = new Page(5, countNum, currentPage);
			_page.setTotalRecord(countNum);
			_page.setPageNumStart(_page.getPageNumStart());
			_page.setPageNumEnd(_page.getPageNumEnd());
			_page.setCurrentPage(currentPage);
			Map<Object,Object> map = new HashMap<Object,Object>();
			map.put("nowpage", _page.getStart());
			map.put("pagesize", _page.getPageRecord());
			List<Userrole> userroleList = null;
			try {
				userroleList = service.allUserrole(map);
			} catch (Exception e) {
				e.printStackTrace();
			}
			modelMap.put("userroleList", userroleList);
			modelMap.put("showPage", _page);
          return "back/jsp/userrole/userroleView";
     }
      
      
      @RequestMapping(value = "/index.do")
      public String index(HttpServletRequest request, HttpSession session){
    	  logger.info("来自IP[" + request.getRemoteHost() + "]的访问");
  		return "index";
    	  
      }
      
      @RequestMapping("/login.do")
  	  public String login(HttpServletRequest request,ModelMap modelMap) {
  		String result = "";
  		// 此处默认有值
  		String username = request.getParameter("username");
  		String mingwen =request.getParameter("password");
  		if(null==mingwen||null==mingwen||"".equals(mingwen)||"".equals(mingwen)){
  			return "redirect:/index.do";
  		}
  		//MD5加密
  		MD5 md = new MD5();
  		String password = md.getMD5ofStr(mingwen);
  		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
  		
  		Subject currentUser = SecurityUtils.getSubject();
		if (!currentUser.isAuthenticated()){
			//未认证
			token.setRememberMe(true);
			try {
				currentUser.login(token);
				ActiveUserDto ActiveUser=(ActiveUserDto)currentUser.getPrincipal();//取回用户信息
				modelMap.put("sysuser", ActiveUser);
				result = "back/jsp/main";
			} catch (Exception e) {
				logger.error(e.getMessage());
				result = "redirect:/index.do";
			}
		}else {
			//已经认证
			result = "back/jsp/main";
		}
		logger.info("返回: " + result);
  		return result;
  	}
      
      
    @RequestMapping("/logout.do")
  	public String logout(HttpSession session)throws Exception{
    	System.out.println("-------------Shiro  logOut------------------>>>");
  		//session失效
  		session.invalidate();
  		return "redirect:/index.do";
  		
  	}
      

      @RequestMapping(value = "/userroleDel{id}.do")
      public String userroleDel(@PathVariable("id")int id ,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             service.delUserrole(id);
             String message1="删除成功";
             String message2="请返回……";
             modelMap.put("message1", message1);
             modelMap.put("message2", message2);
            return "redirect:/userroleView.do";
      }

      @RequestMapping(value ="/userroleGoAdd.do")
             public String userroleGoAdd(ModelMap modelMap,String pageNo, String choice, HttpSession session){
             return "back/jsp/userrole/userroleAdd";
      }

      @RequestMapping(value = "/userroleAdd.do",method=RequestMethod.POST)
      public String userroleAdd(Userrole form,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             service.addUserrole(form);
             String message1="添加成功";
             String message2="请返回……";
             String  url="userroleView.do";
             modelMap.put("message1", message1);
             modelMap.put("message2", message2);
             modelMap.put("url", url);
             return "redirect:/userroleView.do";
      }

      @RequestMapping(value = "/userroleGoUpdate{id}.do")
      public String userroleGoUpdate(@PathVariable("id")int id ,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             Userrole userrole = service.userrole(id);
             modelMap.put("userrole", userrole);
             return "back/jsp/userrole/userroleUpdate";
      }

      @RequestMapping(value = "/userroleUpdate.do",method=RequestMethod.POST)
      public String userroleUpdate(Userrole form,ModelMap modelMap,String pageNo, String choice, HttpSession session){
             service.updateUserrole(form);
             String message1="修改成功";
             String message2="请返回……";
             String  url="userroleView.do";
             modelMap.put("message1", message1);
             modelMap.put("message2", message2);
             modelMap.put("url", url);
             return "redirect:/userroleView.do";
      }

 }
